package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.system.domain.WmsInbound;
import com.ruoyi.system.domain.WmsInventoryHistory;
import com.ruoyi.system.domain.WmsInventoryStatistics;
import com.ruoyi.system.domain.WmsOutbound;
import com.ruoyi.system.enums.OperationType;
import com.ruoyi.system.enums.WmsOutboundStatus;
import com.ruoyi.system.mapper.WmsInboundMapper;
import com.ruoyi.system.mapper.WmsInventoryHistoryMapper;
import com.ruoyi.system.mapper.WmsInventoryStatisticsMapper;
import com.ruoyi.system.mapper.WmsOutboundMapper;
import com.ruoyi.system.service.IWmsOutboundService;
import com.ruoyi.system.utils.GetOrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 出库信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-24
 */
@Service
public class WmsOutboundServiceImpl implements IWmsOutboundService
{
    @Autowired
    private WmsOutboundMapper wmsOutboundMapper;
    @Autowired
    private GetOrderNoUtil getOrderNoUtil;
    @Autowired
    private WmsInventoryHistoryMapper wmsInventoryHistoryMapper;
    @Autowired
    private WmsInventoryStatisticsMapper wmsInventoryStatisticsMapper;
    @Autowired
    private WmsInboundMapper wmsInboundMapper;

    /**
     * 查询出库信息
     *
     * @param id 出库信息主键
     * @return 出库信息
     */
    @Override
    public WmsOutbound selectWmsOutboundById(Long id)
    {
        WmsOutbound wmsOutbound = wmsOutboundMapper.selectWmsOutboundById(id);

        WmsInventoryHistory wmsInventoryHistory = new WmsInventoryHistory();

        wmsInventoryHistory.setOperationId(id);


        List<WmsInventoryHistory> wmsInventoryHistories = wmsInventoryHistoryMapper.selectWmsInventoryHistoryDetailListFromOutbound(wmsInventoryHistory);


        wmsOutbound.setInventoryHistoryList(wmsInventoryHistories);

        return wmsOutbound;
    }

    /**
     * 查询出库信息列表
     *
     * @param wmsOutbound 出库信息
     * @return 出库信息
     */
    @Override
    public TableDataInfo<WmsOutbound> selectWmsOutboundList(WmsOutbound wmsOutbound, PageQuery pageQuery)
    {
        Page<WmsOutbound> page = wmsOutboundMapper.selectWmsOutboundList(pageQuery.build(),wmsOutbound);
        return TableDataInfo.build(page);
    }

    /**
     * 新增出库信息
     *
     * @param wmsOutbound 出库信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertWmsOutbound(WmsOutbound wmsOutbound)
    {
        boolean flag = false;

        //新增出库信息
        if(WmsOutboundStatus.SHIPPED.getCode().equals(wmsOutbound.getOutboundStatus())){
            wmsOutbound.setCreateTime(new Date());
            wmsOutbound.setOutboundUserId(LoginHelper.getUserId());
            flag = true;
        }

        int i = wmsOutboundMapper.insertWmsOutbound(wmsOutbound);
        //插入二维码(待定)

        //新增入库商品明细信息
        for (WmsInventoryHistory wmsInventoryHistory : wmsOutbound.getInventoryHistoryList()){

            wmsInventoryHistory.setInboundId(wmsInventoryHistory.getId());
            //修改库存记录的出库数量字段
            editOutboundTotalById(wmsInventoryHistory);

            wmsInventoryHistory.setOperationId(wmsOutbound.getId());
            wmsInventoryHistory.setOperationNo(wmsOutbound.getOutboundOrderNo());
            wmsInventoryHistory.setOperationType(OperationType.OUTBOUND.getCode());
            wmsInventoryHistory.setCreateTime(new Date());
            wmsInventoryHistory.setId(null);
            wmsInventoryHistoryMapper.insertWmsInventoryHistory(wmsInventoryHistory);


            if(flag){
                //修改库存统计表的库存数量
                WmsInventoryStatistics wmsInventoryStatistics = new WmsInventoryStatistics();

                wmsInventoryStatistics.setProductId(wmsInventoryHistory.getProductId());
                wmsInventoryStatistics.setInventoryTotal(wmsInventoryHistory.getTotalQuantity());
                wmsInventoryStatisticsMapper.editSubtractInventoryStatistics(wmsInventoryStatistics);
            }
        }

        return i;
    }

    /**
     * 修改出库信息
     *
     * @param wmsOutbound 出库信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateWmsOutbound(WmsOutbound wmsOutbound)
    {
        boolean flag = false;

        //新增出库信息
        if(WmsOutboundStatus.SHIPPED.getCode().equals(wmsOutbound.getOutboundStatus())){
            wmsOutbound.setCreateTime(new Date());
            wmsOutbound.setOutboundUserId(LoginHelper.getUserId());
            flag = true;
        }

        WmsInventoryHistory wmsInventoryHistory = new WmsInventoryHistory();
        wmsInventoryHistory.setOperationId(wmsOutbound.getId());

        List<WmsInventoryHistory> wmsInventoryHistorieList = wmsInventoryHistoryMapper.selectWmsInventoryHistoryDetailListFromOutbound(wmsInventoryHistory);

        //先将数量还原回去
        for (WmsInventoryHistory inventoryHistory : wmsInventoryHistorieList) {
            WmsInventoryHistory editInventoryHistory = new WmsInventoryHistory();
            editInventoryHistory.setId(inventoryHistory.getInboundId());
            editInventoryHistory.setOutboundTotal(inventoryHistory.getTotalQuantity());
            wmsInventoryHistoryMapper.editSubtractOutboundTotal(editInventoryHistory);
        }

        Long[] ids = wmsInventoryHistorieList.stream().map(WmsInventoryHistory::getId)  // 提取每个对象的id
                .toArray(Long[]::new);           // 转换为Long数组

        //删除之前的商品明细
        wmsInventoryHistoryMapper.deleteWmsInventoryHistoryByIds(ids);

        //再将新的商品明细插入
        for (WmsInventoryHistory wmsInventoryHistoryDetail : wmsOutbound.getInventoryHistoryList()) {

//            wmsInventoryHistoryDetail.setInboundId(wmsInventoryHistoryDetail.getId());
            //修改库存记录的出库数量字段
            editOutboundTotalById(wmsInventoryHistoryDetail);

            wmsInventoryHistoryDetail.setOperationId(wmsOutbound.getId());
            wmsInventoryHistoryDetail.setOperationNo(wmsOutbound.getOutboundOrderNo());
            wmsInventoryHistoryDetail.setOperationType(OperationType.OUTBOUND.getCode());
            wmsInventoryHistoryDetail.setCreateTime(new Date());
            wmsInventoryHistoryDetail.setId(null);
            wmsInventoryHistoryMapper.insertWmsInventoryHistory(wmsInventoryHistoryDetail);

            if(flag){
                //修改库存统计表的库存数量
                WmsInventoryStatistics wmsInventoryStatistics = new WmsInventoryStatistics();

                wmsInventoryStatistics.setProductId(wmsInventoryHistory.getProductId());
                wmsInventoryStatistics.setInventoryTotal(wmsInventoryHistory.getTotalQuantity());
                wmsInventoryStatisticsMapper.editSubtractInventoryStatistics(wmsInventoryStatistics);
            }
        }


        return wmsOutboundMapper.updateWmsOutbound(wmsOutbound);
    }

    /**
     * 批量删除出库信息
     *
     * @param ids 需要删除的出库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsOutboundByIds(Long[] ids)
    {
        return wmsOutboundMapper.deleteWmsOutboundByIds(ids);
    }

    /**
     * 删除出库信息信息
     *
     * @param id 出库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsOutboundById(Long id)
    {
        return wmsOutboundMapper.deleteWmsOutboundById(id);
    }

    /**
     * 获取入库单编号
     * @return
     */
    @Override
    public WmsOutbound getOutboundOrderNo() {
        WmsOutbound wmsOutbound = new WmsOutbound();
        wmsOutbound.setOutboundOrderNo(getOrderNoUtil.getOrderNo("CK-"));
        return wmsOutbound;
    }

    @Override
    public void clearOrderNo(){
        getOrderNoUtil.clearOrderNo();
    }

    @Override
    public TableDataInfo<WmsOutbound> getInboundProducts(WmsOutbound wmsOutbound,PageQuery pageQuery){
        Page<WmsOutbound> page = wmsOutboundMapper.getInboundProducts(pageQuery.build(),wmsOutbound);
        return TableDataInfo.build(page);
    }

    @Override
    public List<WmsOutbound> getInboundProductsByWmsInventoryCheckEdit(WmsOutbound wmsOutbound){
        return wmsOutboundMapper.getInboundProductsByWmsInventoryCheckEdit(wmsOutbound);
    }

    @Override
    public List<WmsOutbound> getInboundProductsByWmsInventoryCheckAdd(WmsOutbound wmsOutbound){
        return wmsOutboundMapper.getInboundProductsByWmsInventoryCheckAdd(wmsOutbound);
    }

    @Override
    public TableDataInfo<WmsOutbound> getInboundProductsByPage(WmsOutbound wmsOutbound, PageQuery pageQuery){
        Page<WmsOutbound> page = wmsOutboundMapper.getInboundProductsByPage(pageQuery.build(),wmsOutbound);
        return TableDataInfo.build(page);
    }

    /**
     * 修改库存记录和入库记录的出库数量字段
     * @param wmsInventoryHistory
     * @return
     */
    @Override
    public void editOutboundTotalById(WmsInventoryHistory wmsInventoryHistory){

        WmsInventoryHistory editWmsInventoryHistory = new WmsInventoryHistory();
        editWmsInventoryHistory.setOutboundTotal(wmsInventoryHistory.getTotalQuantity());
        editWmsInventoryHistory.setId(wmsInventoryHistory.getInboundId());
        //修改库存记录的出库数量字段
        wmsInventoryHistoryMapper.editOutboundTotal(editWmsInventoryHistory);

        //查出商品id
        editWmsInventoryHistory = wmsInventoryHistoryMapper.selectWmsInventoryHistoryById(wmsInventoryHistory.getInboundId());

        //入库记录的出库数量字段
        WmsInbound wmsInbound = new WmsInbound();
        wmsInbound.setOutboundTotal(wmsInventoryHistory.getTotalQuantity());
        wmsInbound.setId(editWmsInventoryHistory.getOperationId());
        wmsInboundMapper.editOutboundTotal(wmsInbound);

    }

    /**
     * 作废
     */
    @Transactional
    public int cancellation(Long id){

        WmsOutbound wmsOutbound = new WmsOutbound();
        wmsOutbound.setId(id);
        wmsOutbound.setOutboundStatus(WmsOutboundStatus.INVALID.getCode());


        WmsInventoryHistory wmsInventoryHistory = new WmsInventoryHistory();
        wmsInventoryHistory.setOperationId(wmsOutbound.getId());

        List<WmsInventoryHistory> wmsInventoryHistorieList = wmsInventoryHistoryMapper.selectWmsInventoryHistoryDetailListFromOutbound(wmsInventoryHistory);

        //对出库的商品进行累减操作
        for (WmsInventoryHistory inventoryHistory : wmsInventoryHistorieList) {
            WmsInventoryHistory editInventoryHistory = new WmsInventoryHistory();
            editInventoryHistory.setId(inventoryHistory.getInboundId());
            editInventoryHistory.setOutboundTotal(inventoryHistory.getTotalQuantity());
            wmsInventoryHistoryMapper.editSubtractOutboundTotal(editInventoryHistory);
        }
        //修改出库信息的状态，为作废
        wmsOutbound.setCreateTime(new Date());
        wmsOutbound.setOutboundUserId(LoginHelper.getUserId());
        return wmsOutboundMapper.updateWmsOutbound(wmsOutbound);
    }

}
